// pages/drugList/drugList.js
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    openSwipeCell: false,
    showEmpty: false,
    swipeCellId: "",
    selected: [],
    drugList: []
  },

  async getDrugList() {
    // 获取药品列表
    const res = await app.call({
      path: '/drug/drugList',
      method: 'GET'
    })

    console.log(res)
    if (res.code === 0) {
      console.log(res.data)
      this.setData({
        
        drugList: res.data.drugList
      })
    } else {
      Toast.fail(res.errorMsg);
    }
    this.setData({
      showEmpty: this.data.drugList.length === 0
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getDrugList()
    this.getTabBar().init();
  },

  onOpenSwipeCell(event) {
    this.setData({
      openSwipeCell: true
    })
    console.log(event)
    const id = event.currentTarget.id;
    console.log("id->" + id)
    // 根据id选择instance，id为van-swipe-cell元素设置的id
    let instance = this.selectComponent(`#${event.currentTarget.id}`);
    this.setData({
      swipeCellId: id
    })
  },

  onClose() {
    this.setData({
      close: false
    }); //点击取消按钮，弹窗隐藏
  },

  async onCloseSwipeCell(event) {
    const {
      position,
      instance
    } = event.detail;
    console.log(event)
    const index = event.currentTarget.dataset.index;
    const drugList = this.data.drugList.slice(); // 获取用药计划列表副本
    const delName = drugList[index].drugName
    switch (position) {
      case 'left':
        this.setData({
          show: true
        })
        instance.close();
        break
      case 'cell':
        instance.close();
        break;
      case 'right':
        Dialog.confirm({
          message: '确定删除 ' + delName + ' 吗？',
        }).then(async () => {
          const res = await app.call({
            path: 'drug/deleteDrug?id=' + this.data.swipeCellId,
            method: 'DELETE'
          })
          console.log(res)
          if (res.code === 0) {
            this.getDrugList()
            Toast.success(delName + '\n删除成功');
          } else {
            Toast.fail(res.errorMsg);
          }
          console.log(drugList)
          if (this.data.drugList.length === 0) {
            this.setData({
              showEmpty: true
            })
          }
          instance.close();
        }).catch(() => {
          // on cancel
        });
        break
    }
    this.setData({
      openSwipeCell: false
    })
  },

  async updateDrugSurplus() {
    // 修改药品余量
    const res = await app.call({
      path: '/drug/updateDrugSurplus',
      method: 'PUT',
      data: {
        id: this.data.swipeCellId,
        surplus: this.data.value
      }
    })

    console.log(res)
    if (res.code === 0) {
      Toast.success("修改成功");
      this.getDrugList()
    } else {
      Toast.fail(res.errorMsg);
    }
    this.setData({
      show: false,
      openSwipeCell: false
    })
  },

  closeDialog() {
    this.setData({
      show: false
    })
  },

  onClickSwipeCell(event) {
    if (this.data.openSwipeCell !== true) {
      const id = event.currentTarget.id;
      console.log("id->" + id)
      wx.navigateTo({
        url: '/pages/drugDetail/drugDetail?from=drugList&id=' + id,
      })
    }
  },

  gotoAddDrug() {
    wx.navigateTo({
      url: '/pages/editDrug/editDrug?from=addDrug',
    })
  }
})